/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Course;
import core.Program;
import core.User;
import db.JDBC.JdbcDaoFactory;
import db.JDBC.core.UserJdbcDAOCore;
import java.util.Enumeration;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import web.SuperSchoolUserObject;

/**
 *
 * @author Anton
 */

public class ProfileBean extends BaseBean
{
    
    @PostConstruct
    public void init()
    {
        initTree();
    }

    public void removeProgram(ActionEvent event)
    {
        Long programId = (Long)event.getComponent().getAttributes().get("programId");
        Integer treeId = (Integer)event.getComponent().getAttributes().get("treeId");

        if(programId != null)
        {
            Program program = JdbcDaoFactory.getInstance().getProgramJdbcDao().getProgramById(programId);
            if(program != null)
            {
                User currentUser = sessionBean.getCurrentUser();
                if(currentUser != null)
                    UserJdbcDAOCore.getInstance().deleteProgramFromUser(currentUser, program);
            }
        }
        if(treeId != null)
        {
            DefaultMutableTreeNode node = findTreeNode(treeId.toString());
                if(node != null)
                {
                    model.removeNodeFromParent(node);
                    selectedUserObject = null;
                    valueChangeEffect.setFired(false);
                }
        }
    }

    public void removeCourse(ActionEvent event)
    {
        Long courseId = (Long)event.getComponent().getAttributes().get("courseId");
        Integer treeId = (Integer)event.getComponent().getAttributes().get("treeId");
        if(courseId != null)
        {
            Course course = JdbcDaoFactory.getInstance().getCourseJdbcDao().getCourseById(courseId);
            if(course != null)
            {
                User currentUser = sessionBean.getCurrentUser();
                if(currentUser != null)
                    UserJdbcDAOCore.getInstance().deleteCourseFromUser(currentUser, course);
            }
        }
        if(treeId != null)
        {
            DefaultMutableTreeNode node = findTreeNode(treeId.toString());
                if(node != null)
                {
                    model.removeNodeFromParent(node);
                    selectedUserObject = null;
                    valueChangeEffect.setFired(false);
                }
        }
    }
    
    private void initTree()
    {
        User currentUser = sessionBean.getCurrentUser();
        if(currentUser != null)
        {
            currentUser = UserJdbcDAOCore.getInstance().getUserByUserId(currentUser.getId());
            counter = 0;
            DefaultMutableTreeNode rootNode = addNode(null, currentUser.getFname() + " " + currentUser.getLname() ,SuperSchoolUserObject.rootTreeType, counter++, null);

            model = new DefaultTreeModel(rootNode);
            selectedUserObject = (SuperSchoolUserObject) rootNode.getUserObject();
            selectedUserObject.setExpanded(true);

            if(currentUser != null)
            {
                if(currentUser.getPrograms() != null)
                {
                    DefaultMutableTreeNode schoolNodePrograms = addNode(rootNode, "Programs", SuperSchoolUserObject.programListTreeType, counter++, null);
                    for (Program p : currentUser.getPrograms())
                    {
                        DefaultMutableTreeNode programNode = addNode(schoolNodePrograms, p.getName(), SuperSchoolUserObject.programTreeType, counter++, p);
                        if(p.getCourses() != null)
                        {
                            for(Course c : p.getCourses())
                            {
                                addNode(programNode, null, SuperSchoolUserObject.courseUnderProgramTreeType, counter++, c);
                            }
                        }
                    }
                }
                if(currentUser.getCourses() != null)
                {
                    DefaultMutableTreeNode schoolNodeCourses = addNode(rootNode, "Courses", SuperSchoolUserObject.courseListTreeType, counter++, null);
                    for(Course c : currentUser.getCourses())
                    {
                        addNode(schoolNodeCourses, null, SuperSchoolUserObject.courseTreeType, counter++, c);
                    }
                }
            }
        }
        else
            model = new DefaultTreeModel(addNode(null, "No user logged in", SuperSchoolUserObject.rootTreeType, 0, null));
    }
}